﻿using System;
using System.Collections.Generic;
using MockDataTransferObject;
using System.Data.SqlClient;
using System.Linq;
using MockCommon;
using System.Data;
using System.Collections.ObjectModel;

namespace MockDataAccess
{
    public class ConsultantDataAccess : BaseDataAccess<Consultant>
    {
        private Collection<SqlParameter> _parameters;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ConsultantDataAccess"/> class.
        /// </summary>
        public ConsultantDataAccess()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsultantDataAccess"/> class.
        /// </summary>
        /// <param name="isFake">if set to <c>true</c> [is fake].</param>
        public ConsultantDataAccess(bool isFake)
            : base(isFake)
        {
        }

        #region R
        /// <summary>
        /// Retrieves all consultants.
        /// </summary>
        /// <returns></returns>
        public Collection<Consultant> RetrieveAllConsultants()
        {
            try
            {
                DataTable dataTable = ExecuteQueryAsDataTable(Constant.ConsultantGetAll, null);
                Collection<AbstractEntity> entities = ConvertToList(dataTable, typeof(Consultant));
                return new Collection<Consultant>(entities.Cast<Consultant>().ToList());
            }
            catch (ArgumentException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
        }

        /// <summary>
        /// Retrieves all consultant users.
        /// </summary>
        /// <returns></returns>
        public DataTable RetrieveAllConsultantUsers()
        {
            return ExecuteQueryAsDataTable(Constant.ConsultantUserGetAll, null);
        }

        /// <summary>
        /// Gets the name of the consultant id by consultant user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public object GetConsultantIdByConsultantUserName(string userName)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });

            return ExecuteQueryAsScalar(Constant.ConsultantGetConsultantIdByUserName, _parameters);
        }

        /// <summary>
        /// Gets the name of the by user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public Consultant GetByUserName(string userName)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });

                DataTable dataTable = ExecuteQueryAsDataTable(Constant.ConsultantGetByUserName, _parameters);
                Collection<AbstractEntity> Consultants = ConvertToList(dataTable, typeof(Consultant));
                if (Consultants != null && Consultants.Count > 0)
                {
                    return (Consultant)Consultants[0];
                }
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            return null;
        }

        /// <summary>
        /// Gets the by filter.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="email">The email.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <param name="isActive">The is active.</param>
        /// <returns></returns>
        public Collection<Consultant> GetByFilter(object userName, object email, object firstName, object lastName, object isActive)
        {
            try
            {

                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterEmail, Value = email });
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterFirstName, Value = firstName });
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterLastName, Value = lastName });
                if (isActive != null)
                {
                    bool isActiveBool = (bool)isActive;
                    _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterIsActive, Value = isActiveBool ? "1" : "0" });
                }

                DataTable dataTable = ExecuteQueryAsDataTable(Constant.ConsultantGetByFilter, _parameters);
                Collection<AbstractEntity> entities = ConvertToList(dataTable, typeof(Consultant));
                return new Collection<Consultant>(entities.Cast<Consultant>().ToList());
            }
            catch (ArgumentException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
        }

        /// <summary>
        /// Gets the count management contract.
        /// </summary>
        /// <param name="consultantId">The consultant id.</param>
        /// <returns></returns>
        public object GetCountManagementContract(string consultantId)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterConsultantId, Value = consultantId });

            return ExecuteQueryAsScalar(Constant.ConsultantGetCountContract, _parameters);
        }
        #endregion
    }
}
